/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FloorUI.java
 *
 * Created on Aug 16, 2011, 6:00:17 PM
 */
package elevatormanagementsystem;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Danny
 */
public class FloorUI extends javax.swing.JPanel {
    int floorNumber;
    Elevator elevator;
    
    final String DOORS_OPEN = "open";
    final String DOORS_CLOSE = "closed";
    final String MOVING_DIR_DOWN = "moving down";
    final String MOVING_DIR_UP = "moving up";
    final String MOVING_DIR_STATIONARY = "stationary";
    final String MAX_FLOOR_MSG = "you are already on the top floor. You can not go any higher";
    final String MIN_FLOOR_MSG = "You are already on the bottom floor. YOu can not go any lower";
    

    /** Creates new form FloorUI */
    public FloorUI(Elevator e, int floorNum) {
        initComponents();
        run();
        floorNumber = floorNum;
        elevator = e;
        currentFloorLbl1.setText("" + e.getCurrentFloor());
        currentFloorLbl1.setText("" + floorNumber);
        
        
        if (floorNumber == elevator.getMaxFloor()) {
            upBttn1.equals(false); // recheck this when at university for issues with .equals and .enabled 
        }
        if (floorNumber == elevator.getMinFloor()) {
            downBttn1.equals(false);            
        }
        
    }
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        currentFloorLbl1 = new javax.swing.JLabel();
        upBttn1 = new javax.swing.JButton();
        downBttn1 = new javax.swing.JButton();

        currentFloorLbl1.setText("1");

        upBttn1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        upBttn1.setText("Up");
        upBttn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                upBttn1ActionPerformed(evt);
            }
        });

        downBttn1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        downBttn1.setText("Down");
        downBttn1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                downBttn1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(131, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(upBttn1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 65, Short.MAX_VALUE)
                    .addComponent(downBttn1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(currentFloorLbl1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addComponent(currentFloorLbl1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(upBttn1, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(downBttn1, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void upBttn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_upBttn1ActionPerformed
    // Image bttn to lit
    if (floorNumber == elevator.getMaxFloor()) {
        System.out.println("on max floor, cant go higher");
    }
    else if (elevator.getCurrentFloor() == floorNumber) {
            try {
                Thread.sleep(500);
                //Doors open
            } catch (InterruptedException ex) {
                Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally {
                //elevator doors open
                elevator.setDoorStatus(DOORS_OPEN);
            }
    }
    else {
        int counter = elevator.getCurrentFloor();
        boolean testCondition = true;
        while (testCondition) {
            if (elevator.getCurrentFloor() > floorNumber) {
                elevator.setMovingDirection(MOVING_DIR_DOWN);
                counter -=1;
                elevator.setCurrentFloor(counter);
                
            }            
            else if (elevator.getCurrentFloor() < floorNumber) {
                elevator.setMovingDirection(MOVING_DIR_UP);
                counter += 1;
                elevator.setCurrentFloor(counter);
            }
            else {
                testCondition = false;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    finally {
                //doors open
                elevator.setDoorStatus(DOORS_OPEN);
                elevator.setMovingDirection(MOVING_DIR_STATIONARY);
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    finally {
                        currentFloorLbl1.setText("" + counter);
                        repaint();
                    }
                    
            }
        }
        
    }
}//GEN-LAST:event_upBttn1ActionPerformed

private void downBttn1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_downBttn1ActionPerformed
    //Down button light up
    if (floorNumber == elevator.getMinFloor()) {
        System.out.println("You are on the bottom floor already");
        // turn into prompt
    }
    else if (elevator.getCurrentFloor() == floorNumber) {
            try {
                Thread.sleep(500);
            } catch (InterruptedException ex) {
                Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
            }
            finally {
                //open doors to elevator image
    }
           
    }
    else {
         int counter = elevator.getCurrentFloor();
         boolean testCondition = true;
         while (testCondition) {
             if (elevator.getCurrentFloor() > floorNumber) {
                 counter -=1;
                 elevator.setCurrentFloor(counter);
             }
             else {
                 testCondition = false;
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    finally {
                        //open doors to elevator
                    }
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException ex) {
                        Logger.getLogger(FloorUI.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    finally {
                        currentFloorLbl1.setText("" + counter);
                        repaint();
                    }
                 
             }
         }
         
    }
   
}//GEN-LAST:event_downBttn1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel currentFloorLbl1;
    private javax.swing.JButton downBttn1;
    private javax.swing.JButton upBttn1;
    // End of variables declaration//GEN-END:variables

    private void run() {
        JFrame floorFrame = new JFrame();
        floorFrame.add(this);
        floorFrame.pack();
        floorFrame.setTitle("Floor UI");
        floorFrame.setVisible(true);
        System.out.println("Reached Run of FloorUI");
        floorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
